package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Myorder;
import com.example.demo.service.OrdersService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("/order")
public class OrdersController {

    @Resource
    OrdersService ordersService;

    //新增
    @PostMapping
    public Result<?> save(@RequestBody Myorder myorder){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Date date = new Date();
        myorder.setOrderDate(formatter.format(date));
        ordersService.insert(myorder);
        return Result.success();
    }


    //修改
    @PutMapping("/shipments")
    public Result<?> updateShipments(@RequestBody Myorder myorder){
        ordersService.updateShipments(myorder);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        ordersService.deleteById(id);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "1000000") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String state,
                              @RequestParam(defaultValue = "") String username){

        Page<Myorder> userPage = ordersService.selectPage(pageNum,pageSize,search,state,username);
        return Result.success(userPage);

    }

    @GetMapping("/myOrder")
    public Result<?> myOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "1000000") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        Page<Myorder> userPage = ordersService.mySelectPage(pageNum,pageSize,search);
        return Result.success(userPage);

    }

}
